from Pyro.EventService.Clients import Subscriber
from threading import Thread
import const, logger, config


class EventSubscriber(Subscriber):
    def __init__(self, blender, crowd, path_manager):
        Subscriber.__init__(self)
        self.__crowd = crowd
        self.__blender = blender
        self.__path_manager = path_manager        
    def event(self, event):
        msg = "Got event " + event.subject + ":" + event.msg
        print "\n\n" + msg + "\n"
        self.logger.debug(msg) 
        with self.__crowd.lock:
            self.__crowd.update_member_list(self.__blender.get_active_members())
            self.__path_manager.reset_paths()

class EventListener(Thread):
    def __init__(self, blender, crowd, path_manager):
        Thread.__init__(self,name="EventListener")
        self.setDaemon(1)
        self.listener = EventSubscriber(blender, crowd, path_manager)
        self.listener.logger = logger.setup_logging("EventListener", config.LOG_EVENTS)                
    def run(self):        
        #listener.subscribeMatch("JoinCommit") #pattern events subscription
        self.listener.subscribe(const.EVENT_JOINCOMMIT)
        self.listener.listen()
        

